package com.tzjw.marketweixin.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器：与业务相关的请求处理
 * @author hayden
 * @since 2023/09/16 14:33
 */
public class CustomInterceptor implements HandlerInterceptor {
    /**
     * handler执行前
     * 预处理操作，权限验证，登陆验证等
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("2.preHandle");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     * handler执行后，返回视图前
     * 修改返回结果模型，日志记录，添加通用数据等
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("4.postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 请求处理完成后
     * 资源释放，或者一些不影响业务响应但是比较耗时的操作等
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("5.afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}


